package br.com.spb.web;

import javax.servlet.http.HttpServletRequest;

import br.com.spb.domain.Municipio;
import br.com.spb.persistence.MunicipioDao;

public class MunicipioDDDWeb {
		private static String TXT;
		private static Integer ddd;
		
		
		/**
		 * @param request
		 * @return
		 */
		public static String pesquisarMunicipioPorDDD(HttpServletRequest request){
			TXT = "";
			preencherParametros(request);
			preencherMunicipios(); 
			return TXT;
		}
		
		/**
		 * 
		 */
		private static void preencherMunicipios(){   
			if(ddd != null){
				for(Municipio municipio : new MunicipioDao().findByParametros(null, null, ddd)){ 
					preencherMunicipio(municipio); 
				} 				
			}
		}
		
		/**
		 * @param municipio
		 * @param codPartido
		 */
		private static void preencherMunicipio(Municipio municipio){ 
			if(cor.equals("linha1")){
				cor = "linha2";
			}else{
				cor = "linha1";
			}
			TXT += "" +
			"<tr class=\""+cor+"\">" +
			"	<td class=\""+cor+"\">"+ municipio.getSigUf() +"</td> " +
			"	<td class=\""+cor+"\">"+ municipio.getNome() +"</td> " + 
			"	<td class=\""+cor+"\">"+ municipio.getNumDDD() +"</td> " + 
			"	<td class=\""+cor+"\">"+ municipio.getNumHabitantesFormatado() +"</td> " + 
			"</tr>";
	 		 
		}
		private static String cor = "linha1";

		/**
		 * @param request
		 */
		private static void preencherParametros(HttpServletRequest request){  
			String numDDD = request.getParameter("ddd");  
			 
			ddd = null;
		    
		    if(numDDD != null && !numDDD.trim().equals("")){
		    	ddd = new Integer(numDDD);
		    }
			
		}
}
